package com.translation;

import java.io.File;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 目录结构扫描
 * 
 * @author knight
 */
public class DirectoryScanner {
	/**
	 * 目录属性列表 "id" : "pkg-Ext.chart", "text" : "chart", "iconCls" : "icon-pkg",
	 * "cls" : "package", "singleClickExpand" : true, children : []
	 */
	/**
	 * 具体项目属性列表 "href" : "output/Ext.chart.Axis.html", "text" : "Axis", "id" :
	 * "Ext.chart.Axis", "isClass" : true, "iconCls" : "icon-cls", "cls" :
	 * "cls", "leaf" : true
	 */

	/**
	 * 扫描目录，生成属性字符串
	 */
	public static String path = "D:\\work_place\\ExtExample\\ext-3.3.0\\examples";

	public static void scan() {
		try {
			File dir = new File(path);
			File[] dirs = dir.listFiles();
			int fileCounter = 0;
			JSONArray result = new JSONArray();
			for (int i = 0; i < dirs.length; i++) {
				File subDir = dirs[i];
				if (subDir.isDirectory()) {// 是否是目录
					File[] files = subDir.listFiles();
					JSONObject dirJson = new JSONObject();
					dirJson.put("id", "pkg-Ext." + subDir.getName());
					dirJson.put("text", subDir.getName());
					dirJson.put("iconCls", "icon-pkg");
					dirJson.put("cls", "package");
					dirJson.put("singleClickExpand", true);

					JSONArray subFiles = new JSONArray();
					for (int j = 0; j < files.length; j++) {
						File f = files[j];
						String fileName = f.getName();
						String filePath = f.getPath();
						if (fileName.indexOf(".html") != -1) {
							JSONObject subFile = new JSONObject();
							subFile.put("href", filePath.substring(filePath
									.indexOf("examples\\") + 9));
							subFile.put("text", fileName.substring(0, fileName
									.indexOf(".html")));
							subFile.put("id", fileName.substring(0, fileName
									.indexOf(".html")));
							subFile.put("isClass", true);
							subFile.put("iconCls", "icon-cls");
							subFile.put("cls", "cls");
							subFile.put("leaf", true);

							subFiles.add(subFile);
							System.out.println(filePath);
							fileCounter++;
						}
					}
					dirJson.put("children", subFiles);
					result.add(dirJson);
				}
			}
			System.out.println("文件总数>" + fileCounter);
			System.out.println(result.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		scan();
	}
}
